package janelaPrincipal;

import conexao.HibernateUtil;
import conexao.tabelas.Contafinanceira;
import conexao.tabelas.Parcela;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.text.Font;
import org.hibernate.Session;

public class JanelaPrincipalController implements Initializable {
    @FXML
    private Font x1;
    @FXML
    private TextField formMovIdentificador;
    @FXML
    private TextField formMovDataMovimentacao;
    @FXML
    private TextField formMovDataCadastro;
    @FXML
    private TextField formMovDescricao;
    @FXML
    private ComboBox<Contafinanceira> formMovContaFinanceira;
    @FXML
    private TextField formMovValorTotal;
    @FXML
    private TextField formMovValorEntrada;
    @FXML
    private TextField formMovNumeroParcelas;
    @FXML
    private TableView<Parcela> formMovParcelas;
    @FXML
    private TextField formMovValorParcelas;
    @FXML
    private ComboBox<?> formMovReceitaDespesa;
    
    private Session conexao;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        conexao = HibernateUtil.getSessionFactory().openSession();
        carregaFormMovContaFinanceira();
        carregaConfiguracaoFormMovParcelas();
    }    

    private void carregaFormMovContaFinanceira() {
        List<Contafinanceira> contas = conexao.createQuery("FROM Contafinanceira").list();
        formMovContaFinanceira.getItems().removeAll(formMovContaFinanceira.getItems());
        contas.stream().forEach((c) -> {
            formMovContaFinanceira.getItems().add(c);
        });
    }

    private void carregaConfiguracaoFormMovParcelas() {
        TableColumn colunaId = new TableColumn("ID");
        colunaId.setCellValueFactory(new PropertyValueFactory<>("idparcela"));
        formMovParcelas.getColumns().add(colunaId);
        
        TableColumn colunaValorInicial = new TableColumn("Valor inicial");
        colunaValorInicial.setCellValueFactory(new PropertyValueFactory<>("valorInicial"));
        formMovParcelas.getColumns().add(colunaValorInicial);
                
        TableColumn colunaVencimento = new TableColumn("Vencimento");
        colunaVencimento.setCellValueFactory(new PropertyValueFactory<>("dataVencimento"));
        formMovParcelas.getColumns().add(colunaVencimento);
    }
    
}